package com.ciwong.mobilelib.widget;

import java.util.Random;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;

/**
 * 音频播放线条波动效果
 *
 * @author res-qqlin
 * @version ciwong v.2.0 2015/8/1
 * @since ciwong v.2.0
 */
public class LineWaveView extends View
{

    protected Paint mPaint = null;// 画笔

    private int minLine = 30;

    private int maxLine = 50;

    private int middleLine = 100;

    // 线条Y坐标
    private Coordinate[] coordinate = new Coordinate[15];

    public LineWaveView(Context context, AttributeSet attrs, int defStyle)
    {
        super(context, attrs, defStyle);
        mPaint = new Paint();// 初始化画笔工具
        mPaint.setAntiAlias(true);// 抗锯齿
        mPaint.setColor(Color.GRAY);// 画笔颜色
        mPaint.setStrokeWidth(3);
    }

    public LineWaveView(Context context, AttributeSet attrs)
    {
        super(context, attrs);
        mPaint = new Paint();// 初始化画笔工具
        mPaint.setAntiAlias(true);// 抗锯齿
        mPaint.setColor(Color.GRAY);// 画笔颜色
        mPaint.setStrokeWidth(3);
    }

    /**
     * 随机的生成表情的位置
     *
     */
    public void addRandomY()
    {
        for (int i = 0; i < 15; i++)
        {
            // int speed = random.nextInt(maxLine);
            // if (speed < 20)
            // {
            // speed += 20;
            // }
            // int tempY = random.nextInt(maxLine) ;
            // int max=20;
            // int min=10;
            // Random random = new Random();

            // int tempY = random.nextInt(maxLine)%(maxLine-minLine+1) +
            // minLine;

            int tempY = (int) (Math.random() * minLine) + maxLine;
            coordinate[i] = new Coordinate(middleLine - tempY, tempY
                    + middleLine);
        }
    }

    @Override
    protected void onDraw(Canvas canvas)
    {
        super.onDraw(canvas);
        // int x = 40 * i;
        //
        // canvas.drawLine(x, y, x, y + 210, mPaint);// 绘制线条
        // if (expressions[i] != null)
        // {
        // expressions[i].coordinate.y += expressions[i].speed + 15;
        // canvas.drawBitmap(expressionBitmaps[i],
        // expressions[i].coordinate.x,
        // ((float) expressions[i].coordinate.y) - 140, mPaint);
        // }
        for (int i = 0; i < 15; i++)
        {
            int x = 40 * (i + 1);
            if (coordinate[i] != null)
            {
                canvas.drawLine(x, coordinate[i].fromY, x, coordinate[i].toY,
                        mPaint);// 画线条
            }
        }
    }

    class Coordinate
    {
        public int fromY;

        public int toY;

        public Coordinate()
        {
        }

        public Coordinate(int newFromY, int newToY)
        {
            fromY = newFromY;
            toY = newToY;
        }
    }

}
